//
// Created by xili on 2024/8/4 18:25.
// Go big or go home.
//
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    bool carPooling(vector<vector<int>> &trips, int capacity) {
        vector<int> diff(1001, 0);
        for (auto &item: trips) {
            diff[item[1]] += item[0];
            diff[item[2]] -= item[0];
        }
        if(diff[0]>capacity){
            return false;
        }
        //还原数组
        for (int i = 1; i < 1001; i++) {
            if ((diff[i] += diff[i - 1]) > capacity) {
                return false;
            }
        }
        return true;
    }
};